﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Easy.BLL;
using Easy.Model;
using Easy.Operation;
using Microsoft.JScript;

namespace Easy.Web.Pages.EntityPages
{
    public partial class AddTemplatePage : BasePage
    {
        public string Fields = "";
        public string entityname = "";
        public string entityid = "";
        public string template = "";
        public string viewname = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            entityname = Request.Params["entityname"];
            entityid = Request.Params["Id"];
            viewname = Request.Params["viewname"] == null ? "" : Request.Params["viewname"];

            ViewMapClass list = EntitySetting_Function.GetViewMapInfo(entityname, viewname);

            Core core = new Core(entityname);
            Dictionary<string, Field> FieldsAll = core.GetViewFields(viewname);
            foreach (var item in FieldsAll)
            {
                Fields += string.Format("<li {3} role='presentation' okey='{2}' atttype={1} class='active'><a href='#'>{0}</a></li>",
                  item.Value.DisplayName,
                  item.Value.Type,
                  item.Value.Name,
                  string.Format("_innerHTML=\"{0}\"", Server.HtmlEncode(getHTML(item.Value.Name, item.Value.DisplayName, item.Value.Type.ToString(), "")))
             );
            }
        }

        private string getHTML(string id, string name, string type, string entity)
        {
            string reHTML = "";
            reHTML = TempleteApp.Str(id, name, type, false);
            
           
            return reHTML;
        }

        protected void SaveFormStructure_Click(object sender, EventArgs e)
        {
            string formHtml = HttpUtility.HtmlDecode(content.Text);
           // fbb.SaveFormStructure(formname, formHtml, System.Web.HttpUtility.UrlDecode(buttons.Value), jscontent_Src.Text, jscontent_beforeLoad.Text, jscontent_afterLoad.Text, jscontent_beforeSave.Text, jscontent_afterSave.Text);
            buttons.Value = buttons.Value;
            content.Text = formHtml;
        }

    }

    public static class TempleteApp
    {
         public static string Str(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}' >" +
                "<label for='{0}' class='control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>" +
                //"<div class='col-md-10'>\n" +
                   // "<input type='input' class='form-control' id='{0}' attname='{0}' atttype='{2}' />\n" +
                //"</div>\n" +
            "</div>";
            return string.Format(templete, id, name, type);
        }
        
    }
    
}